<?php
// MIM theme functions
function mim_mobile_css() {
	header("Cache-Control: must-revalidate");
	header("Expires: " . gmdate( "D, d M Y H:i:s", strtotime('+1 week')  ) . " GMT");
	header("Content-type: text/css; charset=UTF-8");

	$color_bang    =  attribute_escape(get_option('mim_color_b'));
	$color_dark    =  attribute_escape(get_option('mim_color_d'));
	$color_light   =  attribute_escape(get_option('mim_color_l'));
	$gray_dark     =  attribute_escape(get_option('mim_gray_d'));
	$gray_medium   =  attribute_escape(get_option('mim_gray_m'));
	$gray_light    =  attribute_escape(get_option('mim_gray_l'));

	$stylesheet = "
		body
		{
			background-color:#FFFFFF;
			color:#000000;
			font-family:verdana, sans-serif;
			font-size:small;
			line-height:150%;
			margin:0px;
			padding:0px;
			text-align:left;
		}

		body a,
		body a:link
		{
			color:#$color_dark;
			padding:1px;
		}

		body a:visited
		{
			color:#$color_dark;
		}

		body a:hover,
		body a:active
		{
			background-color:#$color_dark;
			color:#$color_light;
		}

		code,
		pre,
		form input.text,
		form textarea.text,
		form input#s
		{
			font-family:'courier new', courier, monospace;
		}

		code,
		pre
		{
			white-space:normal;
		}

		div#t,
		div#c,
		div#f,
		div#z
		{
			padding:2px;
		}

		div#t
		{
			background-color:#$color_dark;
			color:#$color_light;
			padding:5px;
		}

		div#c
		{
			margin:10px 0px;
		}

		div#bd
		{
			font-size:xx-small;
		}

		div#z
		{
			background-color:#$gray_light;
			border-top:1px solid #$gray_dark;
			color:#$gray_dark;
			margin-top:10px;
		}

		div.ad
		{
			margin-top:5px;
		}

		body.blog div.ad
		{
			margin:0 0 10px 0;
		}

		div.ec
		{
			margin:0px 0px 10px 0px;
		}

		div.fl
		{
			margin-bottom:10px;
		}

		div.pt
		{
			margin:10px 0px;
			font-weight:bold;
		}

		div.nav
		{
			background-color:#$gray_light;
			color:#$gray_dark;
			margin-top:10px;
			padding:2px;
		}

		div.ut
		{
			background-color:#$gray_light;
			font-weight:bold;
			padding:2px;
		}

		div.ut span.utt
		{
			color:#$color_bang;
		}

		div.zt
		{
			font-size:x-small;
			margin:5px;
		}

		h1,
		h2,
		h3,
		h4,
		h5,
		h6
		{
			line-height:150%;
		}

		h1
		{
			font-size:small;
			margin:0px;
		}

		body.page h1,
		body.single h1
		{
			font-size:xx-small;
			font-weight:normal;
		}

		h2
		{
			font-size:small;
			margin-bottom:0px;
		}

		h2 span,
		div.ct span.cm,
		span.ak
		{
			color:#$gray_medium;
			font-size:xx-small;
			font-weight:normal;
		}

		h3
		{
			font-size:small;
			margin:0px;
		}

		div#c h3,
		div#c h4,
		div#c h5,
		div#c h6
		{
			margin:10px 0px 0px 0px;
		}

		img
		{
			border:none;
		}

		p
		{
			margin:15px 0px;
		}

		p.em {
			color:#$gray_medium;
			font-size:x-small;
			font-style:italic;
		}

		p.pp
		{
			margin:5px 0px;
			text-align:center;
		}

		form,
		img
		{
			max-width:98%;
		}

		form
		{
			margin:0px;
			padding:0px;
			width:98%;
		}

		form fieldset
		{
			margin:5px 0px;
			padding:4px 2px;
		}

		form fieldset legend
		{
			font-weight:bold;
		}

		form div.form-option
		{
			margin:10px 0;
		}

		form input.text,
		form textarea.text
		{
			font-size:small;
			width:98%;
		}

		form input.check
		{
			font-size:x-small;
		}

		form input.button
		{
			background-color:#$color_dark;
			color:#$color_light;
			margin-left:2px;
		}

		form input#s
		{
			font-size:small;
		}

		form p
		{
			margin-top:0px;
		}

		span.n
		{
			font-weight:bold;
		}

		span.req,
		span.required
		{
			color:#$color_bang;
		}

		table
		{
			font-size:x-small;
			border-spacing:0;
			border:1px solid #000000;
			width:98%;
		}

		table,
		table tr,
		table td
		{
			margin:0px;
			padding:0px;
			vertical-align:top;
		}

		table tr th
		{
			background:#EEEEEE;
			color:#000000;
		}

		table thead tr th
		{
			background:#DDDDDD;
			color:#000000;
		}

		table tfoot tr th
		{
			border-top:1px solid #000000;
		}

		ul
		{
			margin:0px 0px 10px 0px;
			padding:0px 0px 0px 20px;
		}
	";
	echo str_replace( array( "\r", "\n", "\t" ), '', $stylesheet );
	die();
}

function mobile_plugin_xhtml() {
	header('Vary: Accept');
	$charset = get_bloginfo('charset');
	$blog_name = get_bloginfo('name');
	$blog_url = get_bloginfo('url');
	$styles = "<style type=\"text/css\">\n/*<![CDATA[*/\nbody{background-color:#EEEEEE;color:#333333;font-family:verdana, sans-serif;font-size:x-small;line-height:150%;margin:0px;padding:3px;text-align:left;}div,p,h1{background-color:#EEEEEE;color:#333333;font-size:x-small;}\n/*]]>*/\n</style>";
	$mime_type = 'application/xhtml+xml';
	$doctype = '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">';
	header("Cache-Control: Public");
	header("Pragma: Public");
	header("Expires: " . date( "D, d M Y H:i:s", strtotime('+2 weeks')  ) . " GMT");
	header("Content-Type: $mime_type; charset=$charset");
	$about_head = "<?xml version=\"1.0\" encoding=\"$charset\"?>\n$doctype\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta http-equiv=\"Content-Type\" content=\"$mime_type; charset=$charset\" />\n<title>Optimized for mobile devices</title>\n$styles\n</head>\n";
	$about_body = "<body>\n<h1>$blog_name optimized for mobile devices</h1>\n<p>Thank you for visiting $blog_name. This blog has been optimized for mobile devices by <a href=\"http://wp-plugin-dev.googlecode.com/\" rel=\"external\">Make It Mobile</a>, a free open-source plugin for <a href=\"http://wordpress.org/\" title=\"WordPress\" rel=\"external\">WordPress</a>, developed by <a href=\"http://scottwallick.com/?referrer=mim\" title=\"Scott Allan Wallick: An editor making more mistakes than corrections\" rel=\"external\">Scott</a>.</p>\n<p>Happy mobile browsing!</p>\n<p><a href=\"$blog_url/\" title=\"Back to $blog_name\" rel=\"home\">&#171; Return to $blog_name</a></p>\n<hr />\n<p><a href=\"http://ready.mobi/results.jsp?uri=$blog_url/\" rel=\"external nofollow\">ready.mobi</a> | <a href=\"http://validator.w3.org/mobile/?docAddr=$blog_url/\" rel=\"external nofollow\">W3C mobileOK</a></p>\n</body>\n</html>";
	echo $about_head . $about_body;
	die();
}

function mim_body_class( $print = true ) {
	$c = array('mobile');
	is_front_page()  ? $c[] = 'home'        : null;
	is_home()        ? $c[] = 'blog'        : null;
	is_page()        ? $c[] = 'page'        : null;
	is_archive()     ? $c[] = 'archive'     : null;
	is_single()      ? $c[] = 'single'      : null;
	is_attachment()  ? $c[] = 'attachment'  : null;
	is_search()      ? $c[] = 'search'      : null;

	$c = join( ' ', apply_filters( 'body_class',  $c ) );
	return $print ? print($c) : $c;
}

function mim_if_updated_today() {
	global $wpdb;
	$today = date('Y-m-d');
	$updated = $wpdb->get_var("SELECT post_date, ID FROM $wpdb->posts WHERE $wpdb->posts.post_date LIKE '" . $today . "%'");
	if ( $updated > 0 ) {
		if ( is_home() && !is_paged() ) {
			printf( __( "\t\t" . '<div class="ut"><span class="ak">0</span>&#160;<span class="utt">%1$s</span>&#160;<a accesskey="0" href="%2$s" title="%3$s">%4$s</a></div>' . "\n", 'mim' ),
				attribute_escape(get_option('mim_updated_today')),
				get_permalink($updated->ID),
				wp_specialchars( get_the_title($updated->ID), 1 ),
				get_the_title($updated->ID) );
		} elseif ( is_home() && is_paged() ) {
			printf( __( "\t\t" . '<div class="ut"><span class="ak">H</span>&#160;<span class="utt">%1$s</span>&#160;<a accesskey="H" href="%2$s/" title="Home" rel="home">Back to home</a></div>' . "\n", 'mim' ),
				attribute_escape(get_option('mim_updated_today')),
				get_bloginfo('url') );
		}
	}
}

function mim_show_navigation() {
	global $wp_query;
	return ( $wp_query->max_num_pages > 1 ) ? true : false;
}

function mim_page_list() {
	$order = attribute_escape(get_option('mim_page_list_sorting'));
	$exclude = attribute_escape(get_option('mim_page_list_exclude'));
	$paged_list = wp_list_pages("title_li=&sort_column=$order&exclude=$exclude&echo=0");
	$just_links = strip_tags( $paged_list, '<a>' );
	$clean_menu = str_replace( array( "\r", "\n", "\t" ), '', $just_links );
	$with_breaks = str_replace( "</a>", "</a><br />\n", $clean_menu );
	echo str_replace( "<a ", "\t\t\t<a ", $with_breaks );
}

function mim_wp_register( $before = '<li>', $after = '</li>' ) {
	if ( !is_user_logged_in() ) {
		if ( get_option('users_can_register') ) {
			$link = $before . '<span class="ak">A</span>&#160;<a accesskey="A" href="' . get_bloginfo('wpurl') . '/wp-login.php?action=register" title="' . __( 'Register', 'mim' ) . '">' . __( 'Register', 'mim' ) . '</a>' . $after . "\n";
		} else {
			$link = '';
		}
	} else {
		$link = $before .'<span class="ak">A</span>&#160;<a accesskey="A" href="' . get_bloginfo('wpurl') . '/wp-admin/" title="' . __( 'Site Admin', 'mim' ) . '">' . __( 'Site Admin', 'mim' ) . '</a>' . $after . "\n";
	}
	echo apply_filters( 'register', $link );
}

function mim_wp_loginout( $before = '<li>', $after = '</li>' ) {
	if ( ! is_user_logged_in() ) {
		$link = $before .'<span class="ak">L</span>&#160;<a accesskey="L" href="' . get_bloginfo('wpurl') . '/wp-login.php" title="' . __( 'Log in', 'mim' ) . '">' . __( 'Log in', 'mim' ) . '</a>' . $after . "\n";
	} else {
		$link = $before . '<span class="ak">L</span>&#160;<a accesskey="L" href="' . get_bloginfo('wpurl') . '/wp-login.php?action=logout" title="' . __( 'Log out', 'mim' ) . '">' . __( 'Log out', 'mim' ) . '</a>' . $after . "\n";
	}
	echo apply_filters( 'loginout', $link );
}

function get_mim_commenter() {
	$mobilizer = attribute_escape(get_option('mim_comment_mobilizer'));
	$commenter = '<span class="n">' . str_replace( "http://", "$mobilizer", get_comment_author_link() ) . '</span>';
	if ( get_option('mim_comment_avatar_io') > 0 ) {
		$avatar_sz = attribute_escape(get_option('mim_comment_avatar_sz'));
		$avatar = get_avatar( get_comment_author_email(), "$avatar_sz" );
		return $avatar . ' ' . $commenter;
	} else {
		return $commenter;
	}
}

function get_mim_trackbacker() {
	$mobilizer = attribute_escape(get_option('mim_comment_mobilizer'));
	$trackbacker = str_replace( "http://", "$mobilizer", get_comment_author_link() );
	return $trackbacker;
}

function get_footer_text() {
	return attribute_escape(get_option('mim_footer_text'));
}

if ( stristr($_SERVER['REQUEST_URI'], 'mobile.css') ) {
	add_action( 'template_redirect', 'mim_mobile_css' );
} elseif ( stristr($_SERVER['REQUEST_URI'], 'mobile_plugin.xhtml') ) {
	add_action( 'template_redirect', 'mobile_plugin_xhtml' );
}
?>